/**
 * Created by xfz on 2015/5/24.
 */


var efficiency = function () {
    var initData = {};
    return {
        init: function () {
            //是否已知锅炉效率的选择
            $("#isKnow").click(function () {
                if ($('#isKnow').is(':checked')) {
                    $.ajax({
                        url: basePath + "boiler/efficiency/saveTheValueOfIsKnown",
                        dataType: "json",
                        type: "post",
                        data: "isKnown=" + 1
                    });
                    $("#E52").attr("disabled", true);
                    $("#E88").attr("disabled", true);
                    $("#E53").attr("disabled", true);
                    $("#E54").attr("disabled", true);
                    $("#calculateE51").attr("disabled", true);
                    $("#E89").val("");
                    $("#E90").val("");
                    $("#E93").val("");
                    $("#E92").val("");
                    $("#E94").val("");
                    $("#E95").val("");
                }
                else {
                    $.ajax({
                        url: basePath + "boiler/efficiency/saveTheValueOfIsKnown",
                        dataType: "json",
                        type: "post",
                        data: "isKnown=" + 0
                    });
                    efficiency.recover();
                    $("#E52").attr("disabled", false);
                    $("#E88").attr("disabled", false);
                    $("#E53").attr("disabled", false);
                    $("#E54").attr("disabled", false);
                    $("#calculateE51").attr("disabled", false);
                    $("input").autoInject({
                        data:initData,
                        mode:"id"
                    })
                }
            });

            //修改E89数据，重新计算E90、E93、E92、E94、E95并回填
            $('#E89').on("blur.save", function () {
                var $obj = $('#E89');
                var E89 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E89) || E89 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE89",
                    dataType: "json",
                    type: "post",
                    data: "E89=" + E89,
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                }).then(null,null,
                        Metronic.blockUI({
                            boxed:true,
                            message:"相关数值计算中"
                        })
                    )
            });

            //修改E90数据，重新计算E93、E92、E94、E95并回填
            $("#E90").on("blur.save", function () {
                var $obj = $("#E90");
                var E90 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E90) || E90 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE90",
                    dataType: "json",
                    type: "post",
                    data: "E90=" + E90,
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });

                        initData = $.extend(true,{},data.returnData);

                        Metronic.unblockUI();
                    }
                })
            });

            //修改E93数据，重新计算E92、E94、E95并回填
            $("#E93").on("blur", function () {
                var $obj = $("#E93");
                var E93 = $obj.val();
                validate.init($obj);
                if (E93 != "") {
                    if (validate.isNotNumber(E93)) {
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "boiler/efficiency/saveTheValueOfE93",
                        dataType: "json",
                        type: "post",
                        data: "E93=" + E93,
                        success: function (data) {
                            $("input").autoInject({
                                data:data.returnData,
                                mode:"id"
                            });

                            initData = $.extend(true,{},data.returnData);

                            Metronic.unblockUI();
                        }
                    })
                }
                else {
                    validate.invalid();
                }
            });

            //修改E92数据，重新计算E94、E95并回填
            $("#E92").on("blur", function () {
                var $obj = $("#E92");
                var E92 = $obj.val();
                validate.init($obj);
                if (E92 != "") {
                    if (validate.isNotNumber(E92)) {
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "boiler/efficiency/saveTheValueOfE92",
                        dataType: "json",
                        type: "post",
                        data: "E92=" + E92 ,
                        success: function (data) {
                            $("input").autoInject({
                                data:data.returnData,
                                mode:"id"
                            });

                            initData = $.extend(true,{},data.returnData);

                            Metronic.unblockUI();
                        }
                    })
                }
                else {
                    validate.invalid();
                }
            });

            //修改E94数据，重新计算E95并回填
            $("#E94").on("blur", function () {
                var $obj = $("#E94");
                var E94 = $obj.val();
                validate.init($obj);
                if (E94 != "") {
                    if (validate.isNotNumber(E94)) {
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "boiler/efficiency/saveTheValueOfE94",
                        dataType: "json",
                        type: "post",
                        data: "E94=" + E94 ,
                        success: function (data) {
                            $("input").autoInject({
                                data:data.returnData,
                                mode:"id"
                            });
                            initData = $.extend(true,{},data.returnData);
                            Metronic.unblockUI();
                        }
                    })
                }
                else {
                    validate.invalid();
                }
            });

            //修改E95数据
            $("#E95").on("blur", function () {
                var $obj = $("#E95");
                var E95 = $obj.val();
                validate.init($obj);
                if (E95 != "") {
                    if (validate.isNotNumber(E95)) {
                        validate.invalid();
                        return;
                    }
                    validate.valid();
                    $.ajax({
                        url: basePath + "boiler/efficiency/saveTheValueOfE95",
                        dataType: "json",
                        type: "post",
                        data: "E95=" + E95
                    })
                }
                else {
                    validate.invalid();
                }
            });

            //修改E51数据
            $("#E51").on("blur.save", function () {
                var $obj = $("#E51");
                var E51 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E51) || E51 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE51",
                    dataType: "json",
                    type: "post",
                    data: "E51=" + E51
                })
            });

            //E51弹出窗口关闭后，计算E52，并重新计算E89~E95
            $("#modal").on('hide.bs.modal', function () {
                $.ajax({
                    url: basePath + "boiler/efficiency/calculateTheValueOfE52",
                    dataType: "json",
                    async:"true",
                    type: "post",
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        })
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                })
            });

            //修改E52数据，重新计算E89、E90、E93、E92、E94、E95并回填
            $("#E52").on("blur.save", function () {
                var $obj = $("#E52");
                var E52 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E52) || E52 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE52",
                    dataType: "json",
                    type: "post",
                    data: "E52=" + E52,
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                })
            });

            $("#E53").on("blur.save", function () {
                var $obj = $("#E53");
                var E53 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E53) || E53 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE53",
                    dataType: "json",
                    type: "post",
                    data: "E53=" + E53,
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                })
            });
            $("#E54").on("blur.save", function () {
                var $obj = $("#E54");
                var E54 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E54) || E54 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE54",
                    dataType: "json",
                    type: "post",
                    data: "E54=" + E54,
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                })
            });
            $("#E88").on("blur.save", function () {
                var $obj = $("#E88");
                var E88 = $obj.val();
                validate.init($obj);
                if (validate.isNotNumber(E88) || E88 > 100) {
                    validate.invalid();
                    return;
                }
                validate.valid();
                $.ajax({
                    url: basePath + "boiler/efficiency/saveTheValueOfE88",
                    dataType: "json",
                    type: "post",
                    data: "E88=" + E88,
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                })
            });

            $("#calculateTheLastFour").click(function () {
                $.ajax({
                    url: basePath + "boiler/efficiency/calculateTheValueOfE93",
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        $("#E93").val(data[0]);
                        $("#E92").val(data[1]);
                        $("#E94").val(data[2]);
                        $("#E95").val(data[3]);
                    }
                })
            });

            $("#calculateE51").click(function () {
                $.ajax({
                    url: basePath + "boiler/efficiency/calculateTheValueOfE51",
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        $("#E51").val(data.returnMsg);
                    }
                })
            });

            $("#calculateE53").click(function () {
                $.ajax({
                    url: basePath + "boiler/efficiency/initialization",
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        $("#E88").val(data[0]);
                        $("#E89").val(data[1]);
                        $("#E90").val(data[2]);
                        $("#E93").val(data[3]);
                        $("#E92").val(data[4]);
                        $("#E94").val(data[5]);
                        $("#E95").val(data[6]);
                        $("#E53").val(data[7]);
                        $("#E52").val(data[8]);
                    }
                })
            });

            window.onunload = function () {
                $.ajax({
                    url: basePath + "result/calAll",
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    type: "post"
                })
            };

            $("#nextPage").on("click",function (){
               window.location.href = basePath + "boiler/summary?menuId=21";
            });

            //页面重新计算按钮
            $("#calculateAgain").on("click",function(){
                Metronic.blockUI({
                    boxed:true,
                    message:"相关数值计算中"
                })
                $.ajax({
                    url: basePath + "boiler/efficiency/initialization",
                    dataType: "json",
                    //async:"false",
                    type: "post",
                    success: function (data) {
                        $("input").autoInject({
                            data:data.returnData,
                            mode:"id"
                        });
                        initData = $.extend(true,{},data.returnData);
                        Metronic.unblockUI();
                    }
                })
            })
        },
        recover: function () {
            $.get(basePath + "boiler/efficiency/recover", function (data) {
                //alert(JSON.stringify(data));
                if(getValidRecoverVal(data.known)) {
                    $('#isKnow').parent("span").addClass("checked");
                    $("#E52").attr("disabled", true);
                    $("#E88").attr("disabled", true);
                    $("#E53").attr("disabled", true);
                    $("#E54").attr("disabled", true);
                    $("#calculateE51").attr("disabled", true);
                }
                $("#E89").val(getValidRecoverVal(data.e89));
                $("#E90").val(getValidRecoverVal(data.e90));
                $("#E51").val(getValidRecoverVal(data.e51));
                $("#E52").val(getValidRecoverVal(data.e52));
                $("#E88").val(getValidRecoverVal(data.e88));
                $("#E53").val(getValidRecoverVal(data.e53));
                if(getValidRecoverVal(data.e53)==0){
                    $("#E53").val(0);
                }
                $("#E54").val(getValidRecoverVal(data.e54));
                $("#E92").val(getValidRecoverVal(data.e92));
                $("#E93").val(getValidRecoverVal(data.e93));
                $("#E94").val(getValidRecoverVal(data.e94));
                $("#E95").val(getValidRecoverVal(data.e95));
            })
        },

        initParam: function () {
            Metronic.blockUI({
                boxed:true,
                message:"相关数值计算中"
            })
            $.ajax({
                url: basePath + "boiler/efficiency/initialization",
                dataType: "json",
                type: "post",
                success: function (data) {
                    $("input").autoInject({
                        data:data.returnData,
                        mode:"id"
                    });

                    initData = $.extend(true,{},data.returnData);

                    Metronic.unblockUI();
                }
            });
        },

        judgeMethod:function(){
           var count;
            $.get(basePath + "boiler/efficiency/recover", function (data) {
                count = getValidRecoverVal(data.count);
                if(count == 0){
                    efficiency.calculateMiddleResult();
                    efficiency.initParam();
                }else{
                    efficiency.recover();
                }
            })
        } ,

        calculateMiddleResult:function(){
            $.ajax({
                url:basePath+"result/culAll",
                contentType: "application/json;charset=utf-8",
                dataType:"json",
                async: false,
                type: "post",
                success:function(){
                    Metronic.unblockUI();
                }
            })
        }
    }
}();